package com.wp.service.impl;

import com.wp.mapper.TagMapper;
import com.wp.pojo.PageResult;
import com.wp.pojo.Tag;
import com.wp.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;
    /**
     * 新增标签
     * */
    @Override
    public void addTag(Tag tag) {
        Date date = new Date();
        String uid = UUID.randomUUID()
                .toString()
                .replace("-", "");
        tag.setStatus(1)
                .setUid(uid)
                .setClickCount(0)
                .setCreateTime(date)
                .setUpdateTime(date);
        tagMapper.addTag(tag);
    }
    /**
     * 编辑标签
     * */
    @Override
    public void editTag(Tag tag) {
        tag.setUpdateTime(new Date());
        tagMapper.editTag(tag);
    }

    /**
     * 删除标签
     * */
    @Override
    public void deleteTag(String uid) {
        Date date = new Date();
        tagMapper.update(uid,date);
    }
//    @Override
//    public void deleteTag(Tag tag) {
//        tag.setUpdateTime(new Date());
//        tag.setStatus(0);   //0--表示删除
//        tagMapper.deleteTag(tag);
//    }
    /**
     * 获取所有标签列表
     * */
    @Override
    public PageResult getTagList(PageResult pageResult) {
        String query = pageResult.getQuery();
        Integer num = pageResult.getPageNum();
        Integer size = pageResult.getPageSize();
        Integer startNum = (num-1)*size;
        Integer status = 1;
        List<Tag> tags= tagMapper.getTagList(query,startNum,size,status);
        Long total = tagMapper.getTagCount(query,status);
        pageResult.setRows(tags).setTotal(total);
        return pageResult;
    }
}
